<template>
  <div class="manage">
    <el-dialog
      :title="opertateType === 'add' ? '新增用户' : '更新用户'"
      :visible.sync="isShow">
      <compon-form
        :formLabel="opertateFormLabel"
        :form="operateForm"
        :inline="true"
        ref="form">
      </compon-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isShow = false">取消</el-button>
        <el-button type="primary" @click="confirm">确定</el-button>
      </div>
    </el-dialog>
    <div class="manage-header">
      <el-button type="primary" @click="addUser">+ 新增</el-button>
      <compon-form
        :isShow="true"
        :formLabel="formLabel"
        :form="searchFrom"
        :inline="true"
        ref="form">
        <el-button type="primary" @click="getSearchList">搜索</el-button>
      </compon-form>
    </div>
    <el-card class="box-card">
      <el-table
        :data="
          tableData.slice((currentPage - 1) * pageSize, currentPage * pageSize)"
        width="100%"
        height="315px"
        stripe>
        <el-table-column label="序号" width="90" type="index">
          <template slot-scope="scope">
            {{ (currentPage - 1) * pageSize + scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column prop="name" width="100" label="姓名"></el-table-column>
        <el-table-column
          prop="sex"
          width="100"
          label="性别"
          :formatter="formatSex">
        </el-table-column>
        <el-table-column prop="age" width="100" label="年龄"></el-table-column>
        <el-table-column prop="address" label="地址"></el-table-column>
        <el-table-column prop="birth" label="出生日期"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="editUser(scope.row)">编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="removeUser(scope.row.emp_id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="currentPage"
          :page-size="pageSize"
          layout="prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import ComponForm from "@/components/ComponForm.vue";
export default {
  components: {
    ComponForm,
  },
  data() {
    return {
      row: false,
      isShow: false,
      //从第几页开始显示
      currentPage: 1,
      //记录数据数量
      total: 0,
      //每一页数据数量
      pageSize: 5,
      opertateFormLabel: [
        {
          model: "name",
          label: "姓名",
          type: "input",
        },
        {
          model: "age",
          label: "年龄",
          type: "input",
        },
        {
          model: "sex",
          label: "性别",
          type: "select",
          opts: [
            { label: "男", value: 1 },
            { label: "女", value: 2 },
          ],
        },
        {
          model: "birth",
          label: "出生日期",
          type: "date",
        },
        {
          model: "address",
          label: "地址",
          type: "input",
        },
      ],
      operateForm: {
        id: "",
        name: "",
        adderss: "",
        age: "",
        birth: "",
        sex: "",
      },
      rules: {
        name: [],
      },
      formLabel: [
        {
          model: "keyword",
          label: "",
          type: "input",
        },
      ],
      searchFrom: {
        keyword: "",
      },
      opertateType: "add",
      tableData: [],
    };
  },
  methods: {
    getTable() {
      this.$axios
        .get(this.$store.state.address.url + "findAll")
        .then((response) => {
          this.tableData = response.data;
          this.total = this.tableData.length;
          // 时间格式处理
          for (var i = 0; i < this.tableData.length; i++) {
            this.tableData[i].birth = /\d{4}-\d{1,2}-\d{1,2}/g.exec(
              this.tableData[i].birth
            )[0];
          }
        });
    },
    addUser() {
      this.isShow = true;
      (this.opertateType = "add"),
        (this.operateForm = {
          name: "",
          address: "",
          age: "",
          birth: "",
          sex: "",
        });
    },
    subsuccess() {
      this.$message({
        showClose: this.row,
        message: "信息提交成功",
        type: "success",
      });
    },
    suberror() {
      this.$message({
        showClose: true,
        message: "错了哦，这是一条错误消息",
        type: "error",
      });
    },
    confirm() {
      if (this.opertateType === "edit") {
        this.$axios
          .post(this.$store.state.address.url + "editUser", this.operateForm)
          .then((response) => {
            if (response.data === true) {
              this.subsuccess();
              this.getTable();
            }
          })
          .catch(function (error) {
            console.log(error);
          });
        this.isShow = false;
      } else {
        this.$axios
          .post(this.$store.state.address.url + "testEmp", this.operateForm)
          .then((response) => {
            this.row = response.data;
            if (this.row === true) {
              this.subsuccess();
              this.getTable();
            } else {
              this.suberror();
            }
          })
          .catch(function (error) {
            console.log(error);
          });
        this.isShow = false;
      }
    },
    getSearchList() {
      if (this.searchFrom.keyword != "") {
        this.$axios
          .post(this.$store.state.address.url + "searchInfo", this.searchFrom)
          .then((response) => {
            console.log(response.data);
            this.tableData = [];
            this.tableData = response.data;
          });
      }
    },
    handleSizeChange(val) {
      this.pageSize = val;
    },
    handleCurrentChange(val) {
      this.currentPage = val;
    },
    formatSex(row) {
      return row.sex === 1 ? "男" : row.sex === 2 ? "女" : "未填写";
    },
    removeUser(val) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$axios
            .post(this.$store.state.address.url + "removeUser", val, {
              headers: {
                "Content-Type": "application/json",
              },
            })
            .then((response) => {
              if (response.data === 1) {
                this.$message({
                  type: "success",
                  message: "删除成功!",
                });
                this.getTable();
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    editUser(val) {
      this.isShow = true;
      (this.opertateType = "edit"),
        (this.operateForm = {
          emp_id: val.emp_id,
          name: val.name,
          address: val.address,
          age: val.age,
          birth: val.birth,
          sex: val.sex,
        });
    },
  },
  mounted() {
    this.getTable();
  },
  computed: {
    handleIndex() {
      return;
    },
  },
};
</script>

<style lang = "less" scoped>
.manage-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.block {
  float: right;
  margin: 10px 0;
}
</style>